jQuery(document).ready(function(){
    if(document.cookie == "" || document.cookie == undefined || document.cookie ==  null ){
        //alert("COOKIE isNull");
        jQuery(".btn-update,.btn-subscribe,.btn-comment").hide();
    }
    else{
        jQuery.ajax({
            scriptCharset: 'utf-8',
            type: "POST",
            url: "/validation_islogin",
            contentType:"application/json",
            data: JSON.stringify({
                "phonenumber": document.cookie.split(';')[1].split('=')[1],
                "uid": document.cookie.split(';')[0].split('=')[1]
            }),
            dataType: "json",
            success: function(success) {
                if (success.errorcode==0){
                    jQuery(".btn-update,.btn-subscribe,.btn-comment").hide();
                    //jQuery(".btn-update,.btn-subscribe,.btn-comment").attr("disabled", true);
                }
                else{
                    //jQuery(".btn-login,#register_id").hide();
                    //jQuery(".btn-login").attr("disabled",true);
                    jQuery(".btn-login").text("退出");
                    jQuery("#register_id").removeAttr("href");
                    jQuery("#register_id").text("欢迎");
                    //alert("disable login button and register tag");
                    jQuery(".btn-login").on("click", function(event){
                        event.preventDefault();
                        jQuery.ajax({
                            scriptCharset: 'utf-8',
                            type: "POST",
                            url: "/logout",
                            contentType: "application/json",
                            data: JSON.stringify({
                                "uid": document.cookie.split(';')[0].split('=')[1],
                                "phonenumber": document.cookie.split(';')[1].split('=')[1]
                                }),
                            dataType: "json",
                            success: function(result) {
                                //alert("success sent data");
                                if(result.errorcode==1){
                                    //alert(result.errormessage);
                                    window.location.href="/";
                                    }
                                if(result.errorcode==0){
                                    //alert(result.errormessage+"XXX");
                                }
                            },
                            error: function(error) {
                               //alert("Erro : not logout");
                            }
                        });
                    });
                } 
            },
            error: function(error) {
                //alert("keep original page");

            }
        });
    }
});
